package khleuven.mondzorg.domain;

import org.json.JSONObject;

public abstract class ModuleBasedEntry extends Entry {
	
	private Module module;
	private int sequence;
	
	@Deprecated
	public ModuleBasedEntry(EntryType type, Language language,
			DestinationGroup destination, Module module, int seq) throws DomainException {
		super(type, language, destination);
		setModule(module);
		setSequence(seq);
	}

	public ModuleBasedEntry(EntryType type, JSONObject object)  throws DomainException {
		super(type, object);
		setModule(Modules.getInstance().createModule(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_TEST_MODULE)));			
		setSequence(JsonKeysDictionary.getIntItem(object, JsonKeysDictionary.JSON_KEY_TEST_SEQUENCE));
	}

	public Module getModule() {
		return module;
	}

	private void setModule(Module module) {
		this.module = module;
	}

	public int getSequence() {
		return sequence;
	}

	private void setSequence(int sequence) {
		this.sequence = sequence;
	}

	@Override
	public String toString(){
		return super.toString()+getModule().getName()+" - ";
	}

	@Override
	public JSONObject toJSon() throws DomainException {
		JSONObject output = super.toJSon();
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_TEST_MODULE, getModule().getName());
		JsonKeysDictionary.toIntItem(output, JsonKeysDictionary.JSON_KEY_TEST_SEQUENCE, getSequence());
		return output;		
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean gelijk = false;
		if(obj instanceof ModuleBasedEntry){
			ModuleBasedEntry e = (ModuleBasedEntry)(obj);
			if(super.equals(e) && 
				e.getModule().equals(this.getModule()) &&
				e.getSequence() == this.getSequence()){
				gelijk = true;
			}
		}		
		return gelijk;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((module == null) ? 0 : module.hashCode());
		result = prime * result + sequence;
		return result;
	}
}
